// utils/formatter.js
export const formatContent = (content) => {
    if (!content) return [];
    
    const result = [];
    let currentIndex = 0;
    
    // 处理标题格式：### 标题
    const titleRegex = /(###\s+[^\n]+)(\n|$)/g;
    let titleMatch;
    
    while ((titleMatch = titleRegex.exec(content)) !== null) {
      // 添加标题前的普通文本
      if (titleMatch.index > currentIndex) {
        const textContent = content.substring(currentIndex, titleMatch.index);
        result.push(...processTextWithBold(textContent));
      }
      
      // 添加标题（去除###前缀）
      const title = titleMatch[1].replace(/^###\s+/, "");
      result.push({
        type: 'title',
        content: title,
      });
      
      currentIndex = titleMatch.index + titleMatch[0].length;
    }
    
    // 添加剩余文本
    if (currentIndex < content.length) {
      const remainingText = content.substring(currentIndex);
      result.push(...processTextWithBold(remainingText));
    }
    
    return result;
  };
  
  // 处理普通文本中的加粗格式：**加粗文本**
  export const processTextWithBold = (text) => {
    const result = [];
    let lastIndex = 0;
    const boldRegex = /\*\*([^*]+)\*\*/g;
    let boldMatch;
    
    while ((boldMatch = boldRegex.exec(text)) !== null) {
      // 添加加粗前的普通文本
      if (boldMatch.index > lastIndex) {
        result.push({
          type: 'text',
          content: text.substring(lastIndex, boldMatch.index),
        });
      }
      
      // 添加加粗文本
      result.push({
        type: 'bold',
        content: boldMatch[1],
      });
      
      lastIndex = boldMatch.index + boldMatch[0].length;
    }
    
    // 添加剩余文本
    if (lastIndex < text.length) {
      result.push({
        type: 'text',
        content: text.substring(lastIndex),
      });
    }
    
    return result;
  };